package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.util.ListSet;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.plot.PlotUtils;
import com.fr.data.AbstractChartData;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.data.core.DataXMLUtils;
import com.fr.data.util.SortOrder;
import com.fr.data.util.function.DataFunction;
import com.fr.data.util.function.NoneFunction;
import com.fr.util.Utils;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TableChartData extends AbstractChartData
{
  private static final long serialVersionUID = -8007807601939859337L;
  public static final String XML_TAG = "TableChartData";
  private TableData baseTableData;
  private int categoryLabelIndex;
  private int seriesIndex;
  private int[] seriesIndexArray;
  private int summaryColumnIndex = -1;
  private int[] summaryColumnIndexArray = { -1 };
  private DataFunction[] summaryFunctionArray;
  private ListSet categoryLabelListSet = new ListSet();
  private ListSet seriesLabelListSet = new ListSet();
  private Map valueFunctionMap = new HashMap();
  private boolean isCheckedIn = false;
  private boolean isthird = false;
  private static final String NULL_VALUE = "_N_U_L_L_V_A_L_U_E_$";
  private int null_index = 0;

  public TableChartData()
  {
  }

  public TableChartData(TableData paramTableData, int[] paramArrayOfInt1, SortOrder[] paramArrayOfSortOrder, int[] paramArrayOfInt2, DataFunction[] paramArrayOfDataFunction)
  {
    if ((paramArrayOfInt1.length > 0) && (paramArrayOfInt2.length > 0))
      if (paramArrayOfInt1.length == 1)
        if (paramArrayOfInt2.length == 1)
          thirdChartData(paramTableData, paramArrayOfInt1[0], paramArrayOfSortOrder[0], paramArrayOfInt2[0], paramArrayOfDataFunction[0]);
        else
          firstChartData(paramTableData, paramArrayOfInt1[0], paramArrayOfSortOrder[0], paramArrayOfInt2, paramArrayOfDataFunction);
      else
        secondChartData(paramTableData, paramArrayOfInt1, paramArrayOfSortOrder, paramArrayOfInt2[0], paramArrayOfDataFunction[0]);
  }

  private TableData getBaseTableData()
  {
    return this.baseTableData;
  }

  private void firstChartData(TableData paramTableData, int paramInt, SortOrder paramSortOrder, int[] paramArrayOfInt, DataFunction[] paramArrayOfDataFunction)
  {
    this.baseTableData = paramTableData;
    setCategoryLabelIndex(paramInt);
    setSeriesIndexArray(paramArrayOfInt);
    setSummaryColumnIndexArray(paramArrayOfInt);
    setSummaryFunctionArray(paramArrayOfDataFunction);
  }

  private void secondChartData(TableData paramTableData, int[] paramArrayOfInt, SortOrder[] paramArrayOfSortOrder, int paramInt, DataFunction paramDataFunction)
  {
    this.baseTableData = paramTableData;
    setCategoryLabelIndex(paramArrayOfInt[0]);
    setSeriesIndex(paramArrayOfInt[1]);
    setSummaryColumnIndex(paramInt);
    setSummaryFunctionArray(new DataFunction[] { paramDataFunction });
  }

  private void thirdChartData(TableData paramTableData, int paramInt1, SortOrder paramSortOrder, int paramInt2, DataFunction paramDataFunction)
  {
    this.isthird = true;
    this.baseTableData = paramTableData;
    setCategoryLabelIndex(paramInt1);
    setSeriesIndex(paramInt2);
    setSummaryColumnIndex(paramInt2);
    setSummaryFunctionArray(new DataFunction[] { paramDataFunction });
  }

  public int getCategoryLabelIndex()
  {
    return this.categoryLabelIndex;
  }

  public void setCategoryLabelIndex(int paramInt)
  {
    this.categoryLabelIndex = paramInt;
  }

  public int getSeriesIndex()
  {
    return this.seriesIndex;
  }

  public void setSeriesIndex(int paramInt)
  {
    this.seriesIndex = paramInt;
  }

  public int[] getSeriesIndexArray()
  {
    return this.seriesIndexArray;
  }

  public void setSeriesIndexArray(int[] paramArrayOfInt)
  {
    this.seriesIndexArray = paramArrayOfInt;
  }

  public int getSummaryColumnIndex()
  {
    return this.summaryColumnIndex;
  }

  public void setSummaryColumnIndex(int paramInt)
  {
    this.summaryColumnIndex = paramInt;
  }

  public int[] getSummaryColumnIndexArray()
  {
    return this.summaryColumnIndexArray;
  }

  public void setSummaryColumnIndexArray(int[] paramArrayOfInt)
  {
    this.summaryColumnIndexArray = paramArrayOfInt;
  }

  public DataFunction[] getSummaryFunctionArray()
  {
    return this.summaryFunctionArray;
  }

  public void setSummaryFunctionArray(DataFunction[] paramArrayOfDataFunction)
  {
    int i = paramArrayOfDataFunction.length;
    for (int j = 0; j < i; ++j)
      if (paramArrayOfDataFunction[j] == null)
        paramArrayOfDataFunction[j] = new NoneFunction();
    this.summaryFunctionArray = paramArrayOfDataFunction;
  }

  public int getCategoryLabelCount()
  {
    checkInData();
    if (this.categoryLabelListSet == null)
      return 0;
    return this.categoryLabelListSet.size();
  }

  public int getSeriesCount()
  {
    checkInData();
    if (this.seriesLabelListSet == null)
      return 0;
    if (this.isthird)
      return Math.min(this.seriesLabelListSet.size(), 1);
    return this.seriesLabelListSet.size();
  }

  public Object getCategoryLabel(int paramInt)
  {
    checkInData();
    Object localObject = ((ObjectName)this.categoryLabelListSet.get(paramInt)).getObjectThing();
    return ((localObject.toString().startsWith("_N_U_L_L_V_A_L_U_E_$")) ? "" : localObject);
  }

  public Object getSeriesLabel(int paramInt)
  {
    checkInData();
    return ((ObjectName)this.seriesLabelListSet.get(paramInt)).getObjectThing();
  }

  private Object innerGetValueAt(int paramInt1, int paramInt2)
  {
    checkInData();
    ObjectName localObjectName1 = (ObjectName)this.categoryLabelListSet.get(paramInt2);
    ObjectName localObjectName2 = (ObjectName)this.seriesLabelListSet.get(paramInt1);
    XlabelSeriesMultiKey localXlabelSeriesMultiKey = getXlabelSeriesMultiKey(localObjectName1, localObjectName2);
    DataFunction localDataFunction = (DataFunction)this.valueFunctionMap.get(localXlabelSeriesMultiKey);
    if (localDataFunction == null)
      return null;
    return localDataFunction.getResult();
  }

  public Number getValueAt(int paramInt1, int paramInt2)
  {
    Object localObject = innerGetValueAt(paramInt1, paramInt2);
    if (localObject == null)
      return null;
    if (localObject instanceof Number)
      return ((Number)localObject);
    try
    {
      return Double.valueOf(Utils.objectToString(localObject));
    }
    catch (Exception localException)
    {
    }
    return PlotUtils.dateToNumber(innerGetValueAt(paramInt1, paramInt2));
  }

  private void checkInData()
  {
    int i;
    int k;
    Object localObject2;
    ObjectName localObjectName1;
    Object localObject3;
    Object localObject4;
    if (this.isCheckedIn)
      return;
    this.categoryLabelListSet.clear();
    this.seriesLabelListSet.clear();
    this.valueFunctionMap.clear();
    if (this.summaryFunctionArray == null)
    {
      this.isCheckedIn = true;
      return;
    }
    TableData localTableData = getBaseTableData();
    try
    {
      if (this.summaryFunctionArray.length == 1)
      {
        i = localTableData.getRowCount();
        j = 0;
        while (true)
        {
          ObjectName localObjectName2;
          if (j >= i)
            break label646;
          Object localObject1 = localTableData.getValueAt(j, this.categoryLabelIndex);
          localObject2 = localTableData.getValueAt(j, this.seriesIndex);
          localObjectName1 = new ObjectName(this, localObject1);
          if (this.isthird)
          {
            localObject3 = new StringBuffer();
            if (!(this.summaryFunctionArray[0] instanceof NoneFunction))
            {
              ((StringBuffer)localObject3).append(DataUtils.getFunctionDisplayName(this.summaryFunctionArray[0]));
              ((StringBuffer)localObject3).append("[");
            }
            ((StringBuffer)localObject3).append(localTableData.getColumnName(this.seriesIndex));
            if (!(this.summaryFunctionArray[0] instanceof NoneFunction))
              ((StringBuffer)localObject3).append("]");
            localObjectName2 = new ObjectName(this, ((StringBuffer)localObject3).toString());
          }
          else
          {
            localObjectName2 = new ObjectName(this, localObject2);
          }
          localObject3 = getXlabelSeriesMultiKey(localObjectName1, localObjectName2);
          localObject4 = (DataFunction)this.valueFunctionMap.get(localObject3);
          if (localObject4 == null)
          {
            this.categoryLabelListSet.add(localObjectName1);
            this.seriesLabelListSet.add(localObjectName2);
            try
            {
              localObject4 = (DataFunction)this.summaryFunctionArray[0].clone();
            }
            catch (CloneNotSupportedException localCloneNotSupportedException1)
            {
              break label359:
            }
            this.valueFunctionMap.put(localObject3, localObject4);
          }
          if ((this.summaryColumnIndex >= 0) && (this.summaryColumnIndex < localTableData.getColumnCount()))
            try
            {
              ((DataFunction)localObject4).addData(localTableData.getValueAt(j, this.summaryColumnIndex));
            }
            catch (NullPointerException localNullPointerException)
            {
            }
          label359: ++j;
        }
      }
      i = this.seriesIndexArray.length;
      int j = localTableData.getRowCount();
      for (k = 0; k < j; ++k)
      {
        localObject2 = localTableData.getValueAt(k, this.categoryLabelIndex);
        localObjectName1 = new ObjectName(this, localObject2);
        for (int l = 0; l < i; ++l)
        {
          localObject3 = new StringBuffer();
          if (!(this.summaryFunctionArray[l] instanceof NoneFunction))
          {
            ((StringBuffer)localObject3).append(DataUtils.getFunctionDisplayName(this.summaryFunctionArray[l]));
            ((StringBuffer)localObject3).append("[");
          }
          ((StringBuffer)localObject3).append(localTableData.getColumnName(this.seriesIndexArray[l]));
          if (!(this.summaryFunctionArray[0] instanceof NoneFunction))
            ((StringBuffer)localObject3).append("]");
          localObject4 = new ObjectName(this, ((StringBuffer)localObject3).toString());
          XlabelSeriesMultiKey localXlabelSeriesMultiKey = getXlabelSeriesMultiKey(localObjectName1, (ObjectName)localObject4);
          DataFunction localDataFunction = (DataFunction)this.valueFunctionMap.get(localXlabelSeriesMultiKey);
          if (localDataFunction == null)
          {
            this.categoryLabelListSet.add(localObjectName1);
            this.seriesLabelListSet.add(localObject4);
            try
            {
              localDataFunction = (DataFunction)this.summaryFunctionArray[l].clone();
            }
            catch (CloneNotSupportedException localCloneNotSupportedException2)
            {
              break label634:
            }
            this.valueFunctionMap.put(localXlabelSeriesMultiKey, localDataFunction);
          }
          label646: label634: localDataFunction.addData(localTableData.getValueAt(k, this.seriesIndexArray[l]));
        }
      }
    }
    catch (TableDataException localTableDataException)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
    }
    finally
    {
      try
      {
        localTableData.release();
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      }
    }
    this.isCheckedIn = true;
  }

  private XlabelSeriesMultiKey getXlabelSeriesMultiKey(ObjectName paramObjectName1, ObjectName paramObjectName2)
  {
    return new XlabelSeriesMultiKey(this, paramObjectName1, paramObjectName2);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      String str2 = null;
      if (str1.equals("TableData"))
      {
        this.baseTableData = DataXMLUtils.readXMLTableData(paramXMLableReader);
      }
      else if (str1.equals("Attr"))
      {
        if ((str2 = paramXMLableReader.getAttr("categoryLabelIndex")) != null)
          setCategoryLabelIndex(Utils.string2Number(str2).intValue());
        if ((str2 = paramXMLableReader.getAttr("seriesIndex")) != null)
          setSeriesIndex(Utils.string2Number(str2).intValue());
        if ((str2 = paramXMLableReader.getAttr("summaryColumnIndex")) != null)
          setSummaryColumnIndex(Utils.string2Number(str2).intValue());
        if ((str2 = paramXMLableReader.getAttr("isCheckIn")) != null)
          this.isCheckedIn = Boolean.valueOf(str2).booleanValue();
        if ((str2 = paramXMLableReader.getAttr("isThird")) != null)
          this.isthird = Boolean.valueOf(str2).booleanValue();
      }
      else
      {
        Object localObject;
        if (str1.equals("SeriesIndexArray"))
        {
          if (this.seriesIndexArray == null)
          {
            this.seriesIndexArray = new int[] { Integer.valueOf(paramXMLableReader.getAttr("value")).intValue() };
          }
          else
          {
            localObject = this.seriesIndexArray;
            this.seriesIndexArray = new int[localObject.length + 1];
            System.arraycopy(localObject, 0, this.seriesIndexArray, 0, localObject.length);
            this.seriesIndexArray[localObject.length] = Integer.valueOf(paramXMLableReader.getAttr("value")).intValue();
          }
        }
        else if (str1.equals("SummaryColumnIndexArray"))
        {
          if (this.summaryColumnIndexArray == null)
          {
            this.summaryColumnIndexArray = new int[] { Integer.valueOf(paramXMLableReader.getAttr("value")).intValue() };
          }
          else
          {
            localObject = this.summaryColumnIndexArray;
            this.summaryColumnIndexArray = new int[localObject.length + 1];
            System.arraycopy(localObject, 0, this.summaryColumnIndexArray, 0, localObject.length);
            this.summaryColumnIndexArray[localObject.length] = Integer.valueOf(paramXMLableReader.getAttr("value")).intValue();
          }
        }
        else if (str1.equals("SeriesLabelListSet"))
        {
          this.categoryLabelListSet.add(paramXMLableReader.getAttr("value"));
        }
        else if (str1.equals("CategoryLabelListSet"))
        {
          this.seriesLabelListSet.add(paramXMLableReader.getAttr("value"));
        }
        else if (str1.equals("SummaryFunctionArray"))
        {
          if (this.summaryFunctionArray == null)
          {
            this.summaryFunctionArray = new DataFunction[] { DataXMLUtils.readXMLDataFunction(str2) };
          }
          else
          {
            localObject = this.summaryFunctionArray;
            this.summaryFunctionArray = new DataFunction[localObject.length + 1];
            System.arraycopy(localObject, 0, this.summaryFunctionArray, 0, localObject.length);
            this.summaryFunctionArray[localObject.length] = DataXMLUtils.readXMLDataFunction(str2);
          }
        }
        else if (str1.equals("ValueFunctionMap"))
        {
          int i = -1;
          if ((str2 = paramXMLableReader.getAttr("key")) != null)
            i = Integer.parseInt(str2);
          DataFunction localDataFunction = null;
          if ((str2 = paramXMLableReader.getAttr("value")) != null)
            localDataFunction = DataXMLUtils.readXMLDataFunction(str2);
          if (i != -1)
            this.valueFunctionMap.put(new Integer(i), localDataFunction);
        }
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    int i;
    paramXMLPrintWriter.startTAG("TableChartData");
    if (this.baseTableData != null)
      DataXMLUtils.writeXMLTableData(paramXMLPrintWriter, this.baseTableData);
    paramXMLPrintWriter.startTAG("Attr").attr("categoryLabelIndex", this.categoryLabelIndex).attr("seriesIndex", this.seriesIndex).attr("summaryColumnIndex", this.summaryColumnIndex).attr("isCheckIn", this.isCheckedIn).attr("isThird", this.isthird);
    if ((this.seriesIndexArray != null) && (this.seriesIndexArray.length > 0))
      for (i = 0; i < this.seriesIndexArray.length; ++i)
        paramXMLPrintWriter.startTAG("SeriesIndexArray").attr("value", this.seriesIndexArray[i]).end();
    if ((this.summaryColumnIndexArray != null) && (this.summaryColumnIndexArray.length > 0))
      for (i = 0; i < this.summaryColumnIndexArray.length; ++i)
        paramXMLPrintWriter.startTAG("SummaryColumnIndexArray").attr("value", this.summaryColumnIndexArray[i]).end();
    if ((this.categoryLabelListSet != null) && (this.categoryLabelListSet.size() > 0))
      for (i = 0; i < this.categoryLabelListSet.size(); ++i)
        paramXMLPrintWriter.startTAG("CategoryLabelListSet").attr("value", this.categoryLabelListSet.get(i).toString()).end();
    if ((this.seriesLabelListSet != null) && (this.seriesLabelListSet.size() > 0))
      for (i = 0; i < this.seriesLabelListSet.size(); ++i)
        paramXMLPrintWriter.startTAG("SeriesLabelListSet").attr("value", this.seriesLabelListSet.get(i).toString()).end();
    if ((this.summaryFunctionArray != null) && (this.summaryFunctionArray.length > 0))
      for (i = 0; i < this.summaryFunctionArray.length; ++i)
        paramXMLPrintWriter.startTAG("SummaryFunctionArray").attr("function", this.summaryFunctionArray[i].getClass().getName()).end();
    if ((this.valueFunctionMap != null) && (this.valueFunctionMap.size() > 0))
      for (i = 0; i < this.valueFunctionMap.size(); ++i)
        paramXMLPrintWriter.startTAG("ValueFunctionMap").attr("key", i).attr("value", this.valueFunctionMap.get(new Integer(i)).toString()).end();
    paramXMLPrintWriter.end();
  }

  static int access$000(TableChartData paramTableChartData)
  {
    return paramTableChartData.null_index;
  }

  static int access$008(TableChartData paramTableChartData)
  {
    return (paramTableChartData.null_index++);
  }

  class XlabelSeriesMultiKey
    implements Serializable
  {
    private TableChartData.ObjectName XlabelObjectName;
    private TableChartData.ObjectName SeriesMultiName;
    private final TableChartData this$0;

    public XlabelSeriesMultiKey(, TableChartData.ObjectName paramObjectName1, TableChartData.ObjectName paramObjectName2)
    {
      this.this$0 = paramTableChartData;
      setXlabelObjectName(paramObjectName1);
      setSeriesMultiName(paramObjectName2);
    }

    public TableChartData.ObjectName getXlabelObjectName()
    {
      return this.XlabelObjectName;
    }

    public void setXlabelObjectName()
    {
      this.XlabelObjectName = paramObjectName;
    }

    public TableChartData.ObjectName getSeriesMultiName()
    {
      return this.SeriesMultiName;
    }

    public void setSeriesMultiName()
    {
      this.SeriesMultiName = paramObjectName;
    }

    public int hashCode()
    {
      return (this.XlabelObjectName.hashCode() + this.SeriesMultiName.hashCode());
    }

    public boolean equals()
    {
      if (!(paramObject instanceof XlabelSeriesMultiKey))
        return false;
      XlabelSeriesMultiKey localXlabelSeriesMultiKey = (XlabelSeriesMultiKey)paramObject;
      return ((ComparatorUtils.equals(localXlabelSeriesMultiKey.getXlabelObjectName(), getXlabelObjectName())) && (ComparatorUtils.equals(localXlabelSeriesMultiKey.getSeriesMultiName(), getSeriesMultiName())));
    }

    public String toString()
    {
      return getXlabelObjectName().toString() + " " + getSeriesMultiName().toString();
    }
  }

  class ObjectName
    implements Serializable
  {
    private Object objectThing;
    private final TableChartData this$0;

    public ObjectName(, Object paramObject)
    {
      this.this$0 = paramTableChartData;
      setObjectThing(paramObject);
    }

    public Object getObjectThing()
    {
      return this.objectThing;
    }

    public int hashCode()
    {
      return this.objectThing.hashCode();
    }

    public void setObjectThing()
    {
      if ((paramObject == null) || (paramObject.toString().trim().length() == 0))
      {
        StringBuffer localStringBuffer = new StringBuffer("_N_U_L_L_V_A_L_U_E_$");
        localStringBuffer.append(TableChartData.access$000(this.this$0));
        TableChartData.access$008(this.this$0);
        this.objectThing = localStringBuffer.toString();
      }
      else
      {
        this.objectThing = paramObject;
      }
    }

    public boolean equals()
    {
      return ((paramObject instanceof ObjectName) && (ComparatorUtils.equals(((ObjectName)paramObject).getObjectThing(), getObjectThing())));
    }

    public String toString()
    {
      if (this.objectThing == null)
        return "";
      StringBuffer localStringBuffer = new StringBuffer();
      localStringBuffer.append(this.objectThing.toString());
      return localStringBuffer.toString();
    }
  }
}